﻿
using Abp.Configuration;
using Abp.Dependency;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Alipay.AopSdk.AspnetCore;
using AVISOutBound.SSOServer.Configuration;
using AVISOutBound.SSOServer.Localization;
using Castle.MicroKernel.Registration;
using Microsoft.Extensions.Options;
using MyCompanyName.AbpZeroTemplate.Configuration;
using System.Threading.Tasks;
using AVISOutBound.SSOServer.Payment.Wechat;

namespace AVISOutBound.SSOServer
{
    public class SSOServerCoreModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Auditing.IsEnabledForAnonymousUsers = true;

            SSOServerLocalizationConfigurer.Configure(Configuration.Localization);

            Configuration.Settings.Providers.Add<AppSettingProvider>();
        }

        public override void Initialize()
        {
            IocManager.Register<ISettingStore, SettingStore>(DependencyLifeStyle.Transient);
            IocManager.RegisterAssemblyByConvention(typeof(SSOServerCoreModule).GetAssembly());
        }
        public override void PostInitialize()
        {
            base.PostInitialize();

            var settingOptions = InitOptions().GetAwaiter().GetResult();
            IocManager.IocContainer.Register(
              Component.For<IConfigureOptions<AlipayOptions>>().Instance(new ConfigureNamedOptions<AlipayOptions>("", options =>
              {
                  options.AlipayPublicKey = settingOptions.AlipayPublicKey;
                  options.AppId = settingOptions.AppId;
                  options.CharSet = settingOptions.CharSet;
                  options.Gatewayurl = settingOptions.Gatewayurl;
                  options.PrivateKey = settingOptions.PrivateKey;
                  options.SignType = settingOptions.SignType;
                  options.Uid = settingOptions.Uid;
              }))
            );

            var wechatPayOptions = InitWechatPayOptions().GetAwaiter().GetResult();
            IocManager.IocContainer.Register(Component.For<IConfigureOptions<WechatPayOptions>>().Instance(
                new ConfigureNamedOptions<WechatPayOptions>("",
                    options =>
                    {
                        options.AppId = wechatPayOptions.AppId;
                        options.MchId = wechatPayOptions.MchId;
                        options.CertPath = wechatPayOptions.CertPath;
                        options.PayKey = wechatPayOptions.PayKey;
                        options.Password = wechatPayOptions.Password;
                    })));
        }
        private async Task<AlipayOptions> InitOptions()
        {
            var settingManager = IocManager.Resolve<ISettingManager>();
            var _options = new AlipayOptions()
            {
                AlipayPublicKey = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.PublicKey),
                AppId = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Appid),
                CharSet = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Charset),
                Gatewayurl = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Url),
                PrivateKey = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.PrivateKey),
                SignType = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Sign_type),
                Uid = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Alipay.Ppid)
            };
            return _options;
        }

        private async Task<WechatPayOptions> InitWechatPayOptions()
        {
            var settingManager = IocManager.Resolve<ISettingManager>();
            var options=new WechatPayOptions()
            {
                AppId = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Wechat.AppId),
                MchId = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Wechat.MchId),
                CertPath = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Wechat.CertPath),
                PayKey = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Wechat.PayKey),
                Password = await settingManager.GetSettingValueAsync(AppSettings.PaymentConfig.Wechat.Password),
            };

            return options;
        }
    }
}